package com.source.java.thread;

/**
 * @author xubangbang
 * @date 2021/7/5 14:39
 */
public class ThreadCommunication {
    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void wait(Object o, long millis) {
        try {
            log("对对象 %s 进行等待 %s 毫秒", o, millis);
            o.wait(millis);

            log("对对象 %s 进行唤醒", o);
            o.notify();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void log(String format, Object... args) {
        System.out.println(Thread.currentThread().getName() + " : " + String.format(format, args));
    }

    private static final Object lock = new Object();
    private static int now = 10, need = 20;

    public static void main(String[] args) {
        Thread mt = new Thread(){
            @Override
            public void run() {
                log(this.getName());
                synchronized (this) {
                    log("开始阻塞啦");
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    log("阻塞结束啦");
                }
            }
        };
        mt.start();
        sleep(500);
        synchronized (mt) {
            mt.notify();
        }
    }
}
